package demo.model.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import demo.model.dto.IDataSourceTO;
import demo.model.util.SqlBundle;
import java.sql.CallableStatement;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ResourceBundle;


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author Admin
 */
public abstract class ObjectDA {
    
    protected SqlBundle bundle;
    protected IDataSourceTO settingTO;
    public ObjectDA(IDataSourceTO settingTO,SqlBundle bundle) {
        this.settingTO = settingTO;
        this.bundle = bundle;
    }

    protected Connection createConnection() throws Exception {
        String userName = this.settingTO.getUsername();
        String password = this.settingTO.getPassword();
        String url = this.settingTO.getUrl();
        Class.forName(this.settingTO.getDriver()).newInstance();
        Connection conn = (Connection) DriverManager.getConnection(url, userName, password);
        return conn;
    }

    // <editor-fold defaultstate="collapsed" desc="CLOSE. Click on the + sign on the left to edit the code.">
    protected void close(Connection o) throws SQLException {
        if (o != null && !o.isClosed()) {
            o.close();
        }
    }

    protected void close(ResultSet o) throws SQLException {
        if (o != null && !o.isClosed()) {
            o.close();
        }
    }

    protected void close(PreparedStatement o) throws SQLException {
        if (o != null && !o.isClosed()) {
            o.close();
        }
    }

    protected void close(Connection c, PreparedStatement p, ResultSet r) throws SQLException {
        if (r != null && !r.isClosed()) {
            r.close();
        }

        if (p != null && !p.isClosed()) {
            p.close();
        }

        if (c != null && !c.isClosed()) {
            c.close();
        }
    }

    protected void close(Connection c, PreparedStatement p) throws SQLException {
        if (p != null && !p.isClosed()) {
            p.close();
        }

        if (c != null && !c.isClosed()) {
            c.close();
        }
    }

    protected void close(CallableStatement o) throws SQLException {
        if (o != null && !o.isClosed()) {
            o.close();
        }
    }
    // </editor-fold>

    protected PreparedStatement createPrepareStatement(String propertyName, Connection conn) throws SQLException {
        String sqlStmt = this.getSqlBundle().getString(propertyName);
        return conn.prepareStatement(sqlStmt);
    }

    protected CallableStatement createCallableStatement(String propertyName, Connection conn) throws SQLException {
        String sqlStmt = this.getSqlBundle().getString(propertyName);
        return conn.prepareCall(sqlStmt);
    }

    protected ResourceBundle getSqlBundle() {
        return this.bundle.getResoureBundle(this.getSqlBundleName());
    }

    protected abstract String getSqlBundleName();

   
}

